package com.szeldon.pj.jps.exp;

import com.szeldon.pj.jps.datastore.BooleanObject;
import com.szeldon.pj.jps.datastore.SimpleObject;

public class BoolTerminalExpression extends TerminalExpression {

	private boolean value;

	/**
	 * 
	 * @param value
	 */
	public BoolTerminalExpression(boolean value) {
		this.value = value;
	}

	@Override
	public String getValueAsString() {
		return "" + value;
	}

	public boolean getValue() {
		return value;
	}

	@Override
	public int compareTo(SimpleObject object) throws IllegalStateException {
		if (!(object instanceof BooleanObject)) {
			throw new IllegalStateException("BoolTerminalExpression can't be compared with object of "
					+ object.getClass().getName() + " type.");
		}

		boolean objectValue = ((BooleanObject) object).getValue();

		if (value == objectValue) {
			return 0;
		} else if (value == false && objectValue == true) {
			return -1;
		} else {
			return 1;
		}
	}

	@Override
	public int compareTo(TerminalExpression expression) {
		if (expression instanceof BoolTerminalExpression) {
			boolean expressionValue = ((BoolTerminalExpression) expression).getValue();

			if (value == expressionValue) {
				return 0;
			} else if (value == false && expressionValue == true) {
				return -1;
			} else {
				return 1;
			}
		} else {
			throw new IllegalStateException("BoolTerminalExpression can't be compared with expression of "
					+ expression.getClass().getName() + " type.");
		}
	}

}
